package com.zg.datapush.web;

import com.zg.common.core.R;
import com.zg.common.core.dao.system.SysDatasource;
import com.zg.datapush.service.DatasourceQueryService;
import com.zg.datapush.service.DatasourceTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据源查询Controller
 * @author zg
 */
@RestController
@RequestMapping("/datapush/datasourceQuery")
@RequiredArgsConstructor
@Api(tags = "数据源查询")
public class DatasourceQueryController {

    private final DatasourceQueryService datasourceQueryService;
    private final DatasourceTestService datasourceTestService;

    @ApiOperation("获取所有可用数据源")
    @GetMapping("/list")
    public R<List<SysDatasource>> listAvailableDatasources() {
        List<SysDatasource> result = datasourceQueryService.listAvailableDatasources();
        return R.success(result);
    }

    @ApiOperation("根据编码获取数据源")
    @GetMapping("/get/{code}")
    public R<SysDatasource> getByCode(@PathVariable String code) {
        SysDatasource result = datasourceQueryService.getByCode(code);
        return R.success(result);
    }

    @ApiOperation("测试数据源连接")
    @GetMapping("/test/{code}")
    public R<String> testConnection(@PathVariable String code) {
        boolean success = datasourceTestService.testConnection(code);
        if (success) {
            return R.success("连接成功");
        } else {
            return R.error("连接失败");
        }
    }
}
